package org.example.aidemo;

import lombok.extern.slf4j.Slf4j;
import org.example.aidemo.pojo.Result;
import org.example.aidemo.pojo.User;
import org.example.aidemo.service.UserService;
import org.example.aidemo.service.common.AiChatService;
import org.example.aidemo.service.common.QQEmailService;
import org.example.aidemo.utils.AESUtils;
import org.example.aidemo.utils.JwtUtils;
import org.example.aidemo.utils.SecurityUtils;
import org.junit.jupiter.api.Test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.context.TestPropertySource;

import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@TestPropertySource(locations = "classpath:application.properties")
public class a1 {
    @Autowired
    JwtUtils jwtUtils;
    @Autowired
    private ChatModel chatModel;
    @Autowired
    private AiChatService aiChatService;
    @Autowired
    private SecurityUtils securityUtils;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UserService userService;
    @Autowired
    private AESUtils aesUtils;
    @Autowired
    private JavaMailSender javaMailSender;
    @Test
    public void contextLoads() {
        List<String> roles = Arrays.asList("admin", "user");
        String str= jwtUtils.generateToken("admin", roles);
        System.out.println(str);
    }
    @Test
    void testChatAndEmbeddingIntegration() {
        // 测试聊天和嵌入功能的集成
        ChatClient chatClient = ChatClient.builder(chatModel).build();

        // 1. 测试聊天功能
        String chatResponse = chatClient.prompt()
                .user("什么是机器学习？")
                .call()
                .content();

        assertThat(chatResponse).isNotEmpty();
        System.out.println("Chat response: " + chatResponse);
    }
    @Test
    public void testPasswordEncoder() {
        String pwd = "123";
        String encodedPwd = passwordEncoder.encode(pwd);
        System.out.println(encodedPwd);
    }
    @Test
    void testOrderQueryFunction() {
        // 测试订单查询功能
        String response = aiChatService.chatWithFunctions("帮我查一下订单号1的状态");

        assertThat(response).isNotEmpty();
        System.out.println("AI Response: " + response);
    }
    @Test
    public void testSecurityUtils() {
        System.out.println(securityUtils.getCurrentAuthentication());
    }
    @Test
    public void select() {
        Result<User> result = Result.success(userService.findByUsername("qwe"));
            System.out.println("Code: " + result.getCode());
            System.out.println("Message: " + result.getMsg());
            System.out.println("Data: " + result.getData());
            // 或者
            System.out.println("User: " + result.getData().getUsername());
        }
    @Test
    public void testAESUtils() {
        String plainText = "123456";
        String encryptedText = aesUtils.encrypt(plainText);
        System.out.println("Encrypted Text: " + encryptedText);
        String decryptedText = aesUtils.decrypt(encryptedText);
        System.out.println("Decrypted Text: " + decryptedText);
    }
    @Autowired
    private QQEmailService emailService;
    private static final Logger log = LoggerFactory.getLogger(QQEmailService.class);
    @Test
    void testSendEmail() {
        try {
            log.info("邮件发送测试成功");
        } catch (Exception e) {
            log.error("邮件发送测试失败", e);
        }
    }

}